package com.chap5_2;

/**
 * <p>
 * <code>NormalDeadLock</code>
 * </p>
 *
 * @author junbao3
 * TODO
 * @date 2023-03-28 9:57
 * Copyright (C) 2021 IFlyTek. All rights reserved.
 */
public class NormalDeadLock {
    private static Object No13 = new Object();
    private static Object No14 = new Object();

    private static void method1() throws InterruptedException {
        String name = Thread.currentThread().getName();
        synchronized (No13){
            System.out.println(name+" get No13");
            Thread.sleep(100);
            synchronized (No14){
                System.out.println(name+" get No14");
            }
        }
    }

    private static void method2() throws InterruptedException {
        String name = Thread.currentThread().getName();
        synchronized (No14){
            System.out.println(name+" get No14");
            Thread.sleep(100);
            synchronized (No13){
                System.out.println(name+" get No13");
            }
        }
    }


    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    method1();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"张三").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    method2();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"李四").start();
    }
}
